package com.example.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopping.common.consts.MessageEnum;
import com.example.shopping.dao.OrderDao;
import com.example.shopping.pojo.domain.OrderBean;
import com.example.shopping.pojo.vo.DetailsVo;
import com.example.shopping.pojo.vo.OrderVo;
import com.example.shopping.service.OrderDetailsService;
import com.example.shopping.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dream
 * @version 1.0
 * @date 2020/11/9 12:56
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao,OrderBean> implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderDetailsService orderDetailsService;
    /**
     * 查询订单
     * @param userXh
     * @return
     */
    @Override
    public List<OrderVo> getByXh(String userXh) {
        List<OrderVo> list=new ArrayList<>();
        //所有订单的主要信息
        List<OrderBean> order=orderDao.getByXh(userXh);
        for(int i=0;i<order.size();i++){
            OrderVo orderVo=new OrderVo();
            OrderBean orderBean=order.get(i);
            orderVo.setOrderBean(orderBean);
            List<DetailsVo> detailsVo=orderDetailsService.getByOrderId(orderBean.getId());
            orderVo.setOrderList(detailsVo);
            list.add(orderVo);
        }
        return list;
    }

    /**
     * 添加订单
     * @param orderBean
     * @return
     */
    @Override
    public String addOrder(OrderBean orderBean) {
        orderDao.addOrder(orderBean);
        return MessageEnum.ADD_ORDER_SUCCESS;
    }

}
